﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Text.RegularExpressions;
using Shpm.App.Master.Dao;
using Shpm.App.Common.Dao;
using Shpm.App.Master.Bean;

public partial class App_Input_HappyCourseUpdForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyCourseUpdForm));


    private const string DEPART_ID = "151";
    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        initPageControls();
        if(!IsPostBack)
        {
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();
            IList dataList = null;
            //判断是否为审核人员
            int flg = getJobLevel();
            //编辑或审核
            if (Context.Items.Contains("CURRICULUM_ID"))
            {
                //初始化课程信息 
                HappyCourseDao dao = new HappyCourseDao();
                HappyCourseBean bean = new HappyCourseBean();
                bean.CurriculumId = Convert.ToInt32(Context.Items["CURRICULUM_ID"]);
                bean = dao.GetCurriculum(bean);

                if (bean != null)
                {
                    this.labCurriculumId.Text = Convert.ToString(Convert.ToInt32(Context.Items["CURRICULUM_ID"]));
                    this.txtActName.Text = bean.CurriculumName;
                    this.lstActType.SelectedValue = bean.CurriculumType;
                    this.txtBeginDate.Text = DateUtils.convertToString(bean.BeginDate.Value);
                    this.txtEndDate.Text = DateUtils.convertToString(bean.EndDate.Value);
                    this.txtActAddress.Text = bean.CurriculumAddress;
                    this.txtTeacherId.Text = bean.TeacherId.ToString();
                    this.txtRelationName.Text = bean.RelationName;
                    this.txtPhone.Text = bean.RelationTel;
                    this.txtActObject.Text = bean.CurriculumObject;
                    this.txtFeePropagandize.Text = string.Format("{0:N}", bean.FeeWage);
                    this.txtFeeMaterial.Text = string.Format("{0:N}", bean.FeeMaterial);
                    this.txtFeeTraffic.Text = string.Format("{0:N}", bean.FeeTraffic);
                    this.txtFeeLogistics.Text = string.Format("{0:N}", bean.FeeLogistics);
                    this.txtIncome.Text = string.Format("{0:N}", bean.Income);
                    this.txtMemo1.Text = bean.CurriculumContent;
                    this.txtMemo2.Text = bean.CurriculumIntending;
                    this.txtActCount.Text = bean.CurriculumCount.ToString();

                    this.btnOk.Enabled = !bean.CommitFlg.Value;
                    this.btnSubmit.Enabled = !bean.CommitFlg.Value;

                    ViewState["EMPLOYEE_ID"] = bean.EmployeeId;
                }

                //有审批权限或已被提交
                if (flg > 1 || bean.CommitFlg.Value)
                {

                    this.txtActName.ReadOnly = true;
                    this.lstActType.Enabled = false;
                    this.txtBeginDate.Enabled = false;
                    this.txtEndDate.Enabled = false;
                    this.txtActAddress.ReadOnly = true;
                    this.txtTeacherId.ReadOnly = true;
                    this.txtRelationName.ReadOnly = true;
                    this.txtPhone.ReadOnly = true;
                    this.txtActObject.ReadOnly = true;
                    this.txtFeePropagandize.ReadOnly = true;
                    this.txtFeeMaterial.ReadOnly = true;
                    this.txtFeeTraffic.ReadOnly = true;
                    this.txtFeeLogistics.ReadOnly = true;
                    this.txtIncome.ReadOnly = true;
                    this.txtMemo1.ReadOnly = true;
                    this.txtMemo2.ReadOnly = true;
                    this.txtActCount.ReadOnly = true;
                    this.btnDel.Enabled = !bean.CommitFlg.Value;
               

                }

                //审核人员使用的控件
                this.btnAud.Enabled = flg > 1;
                this.tdApprove.Visible = flg > 1;
                


                //初始化下拉列表
                paramBean.Catalog = "T_CURRICULUM.AUDINGTING_TYPE";
                dataList = commonDictionaryDao.FindByCatalog(paramBean);

                lstAud1.DataSource = dataList;
                lstAud1.DataBind();

                if (dataList.Count > 0)
                {
                    lstAud1.SelectedIndex = 0;
                }
                if (!string.IsNullOrEmpty(bean.AuditingType))
                {
                    lstAud1.SelectedValue = bean.AuditingType;
                }
            }
            else 
            {
                this.btnDel.Enabled = false;
                this.btnAud.Enabled = false;
            }


         

            paramBean.Catalog = "CURRICULUM_TYPE";
            dataList = commonDictionaryDao.FindByCatalog(paramBean);

            lstActType.DataSource = dataList;
            lstActType.DataBind();

            this.txtTeacherId.Attributes.Add("onblur", "showTeacherName(this.id,'" + this.txtTeacherName.ClientID + "');");
        }
    }

    // 保存按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }

        if (CheckTeacherId() == false) 
        {
            ((Design_MasterPage)this.Master).InfoMessage = 
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtTeacherId.Text);
            return;
        }

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;


        HappyCourseDao dao      = new HappyCourseDao();
        HappyCourseBean bean    = new HappyCourseBean();
        bean.CurriculumName     = this.txtActName.Text;
        bean.CurriculumType     = this.lstActType.SelectedValue;
        bean.BeginDate          = DateUtils.convertToDate(this.txtBeginDate.Text);
        bean.CurriculumAddress  = this.txtActAddress.Text;
        bean.EndDate            = DateUtils.convertToDate(this.txtEndDate.Text);
        bean.TeacherId          = Convert.ToInt32(this.txtTeacherId.Text);
        bean.CurriculumObject   = this.txtActObject.Text;
        bean.CurriculumCount    = Convert.ToInt32(this.txtActCount.Text.Trim());
        bean.RelationName       = this.txtRelationName.Text;
        bean.RelationTel        = this.txtPhone.Text;
        //bean. = this.txtPrincipalId.Text;
        if (!string.IsNullOrEmpty(this.txtFeePropagandize.Text.Trim()))
            bean.FeeWage            = Convert.ToDecimal(this.txtFeePropagandize.Text.Trim());
        if (!string.IsNullOrEmpty(this.txtFeeMaterial.Text.Trim()))
            bean.FeeMaterial        = Convert.ToDecimal(this.txtFeeMaterial.Text.Trim());
        if (!string.IsNullOrEmpty(this.txtFeeTraffic.Text.Trim()))
            bean.FeeTraffic         = Convert.ToDecimal(this.txtFeeTraffic.Text.Trim());
        if (!string.IsNullOrEmpty(this.txtFeeLogistics.Text.Trim()))
            bean.FeeLogistics       = Convert.ToDecimal(this.txtFeeLogistics.Text.Trim());
        if (!string.IsNullOrEmpty(this.txtIncome.Text.Trim()))
            bean.Income             = Convert.ToDecimal(this.txtIncome.Text.Trim());
        bean.CurriculumContent  = this.txtMemo1.Text;
        bean.CurriculumIntending = this.txtMemo2.Text;
        bean.UserId = loginId;
        bean.CommitFlg = "COMPLETE".Equals((sender as Button).CommandName);
        bean.EmployeeId = emplyId;
        if (string.IsNullOrEmpty(this.labCurriculumId.Text))
        {
            
            int id = (int)dao.InsCurriculum(bean);
            this.labCurriculumId.Text = id.ToString();
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        }
        else 
        {
            bean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
            dao.UpdCurriculum(bean);
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }

        if ("COMPLETE".Equals((sender as Button).CommandName))
        {
            this.btnOk.Enabled = false;
            this.btnSubmit.Enabled = false;
            this.btnDel.Enabled = false;

            //发送消息

            MessageBean beanMsg = new MessageBean();
            beanMsg.Subject = "课程信息确认--" + DateUtils.convertToString(DateTime.Now);
            beanMsg.Message = this.txtActName.Text+"课程已建立，烦请领导审批！";

            beanMsg.MessageType = 3;
            beanMsg.SendDate = DateTime.Now;
            beanMsg.ReplyFlag = false;
            beanMsg.ReaderHistoryFlag = false;
            beanMsg.ImportantFlag = false;
            beanMsg.EmployeeId = emplyId;
            beanMsg.UserId = loginId;

            //公寓主任
            EmployeeDao employeeDao = new EmployeeDao();
            EmployeeBean employeeBean = new EmployeeBean();
            CommonSendMessageUtils sendMsg = null;
            employeeBean.GroupId = 151;
            IList list = employeeDao.FindEmployees(employeeBean);
            for (int i = 0; i < list.Count; i++)
            {
                sendMsg = new CommonSendMessageUtils(beanMsg, (list[i] as EmployeeBean).EmployeeId);
                sendMsg.SendMessage();
            }

           
        }
        else 
        {
            this.btnDel.Enabled = true;
        }
       
    }
    private bool CheckTeacherId()
    {
        CommonSearchDao dao = new CommonSearchDao();
        TeacherBean beanMem = new TeacherBean();
        beanMem.TeacherId = Convert.ToInt32(this.txtTeacherId.Text);
        if (dao.GetTeacher(beanMem) == null)
            return false;
        return true;
    }
    // 删除按钮
    protected void btnDel_Click(object sender, EventArgs e)
    {
        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        bean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        dao.DelCurriculum(bean);

        ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();


        this.labCurriculumId.Text = String.Empty;
        this.txtActName.Text = String.Empty;
        this.lstActType.SelectedIndex = 0;
        this.txtBeginDate.Text = String.Empty;
        this.txtEndDate.Text = String.Empty;
        this.txtActAddress.Text = String.Empty;
        this.txtTeacherId.Text = String.Empty;
        this.txtActObject.Text = String.Empty;
        this.txtFeePropagandize.Text = String.Empty;
        this.txtFeeMaterial.Text = String.Empty;
        this.txtFeeTraffic.Text = String.Empty;
        this.txtFeeLogistics.Text = String.Empty;
        this.txtIncome.Text = String.Empty;
        this.txtMemo1.Text = String.Empty;
        this.txtMemo2.Text = String.Empty;
        this.txtActCount.Text = String.Empty;

        this.btnDel.Enabled = false;
    }

    // 审核按钮
    protected void btnAud_Click(object sender, EventArgs e)
    {

        if (this.lstActType.Items.Count <= 0)
        {
            return;
        }

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
        
        //不是公寓主任不能审核


        EmployeeDao employeeDao = new EmployeeDao();
        EmployeeBean employeeBean = new EmployeeBean();

        employeeBean.GroupId = 151;
        employeeBean.EmployeeId = emplyId;
        IList list = employeeDao.FindEmployees(employeeBean);

        if (list.Count <= 0)
        {
            this.txtActName.Text = mstEmpBean.GroupId;
            ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0901").ToString();
            return;
        }


        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        bean.CurriculumId = Convert.ToInt32(this.labCurriculumId.Text);
        bean.AuditingType = this.lstAud1.SelectedValue;
        bean.AuditingFlg = true;
        bean.CommitFlg = true;
        bean.UserId = loginId;
        dao.UpdCurriculumAuditingType(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();


        //发送审批消息

        MessageBean beanMsg = new MessageBean();
        beanMsg.Subject = "审批课程信息--" + DateUtils.convertToString(DateTime.Now);
        beanMsg.Message = "审批意见："+this.txtActName.Text+ lstAud1.Items[lstAud1.SelectedIndex].Text;

        beanMsg.MessageType = 3;
        beanMsg.SendDate = DateTime.Now;
        beanMsg.ReplyFlag = false;
        beanMsg.ReaderHistoryFlag = false;
        beanMsg.ImportantFlag = false;
        beanMsg.EmployeeId = emplyId;
        beanMsg.UserId = loginId;

        CommonSendMessageUtils sendMsg = new CommonSendMessageUtils(beanMsg, Convert.ToString(ViewState["EMPLOYEE_ID"]));
        sendMsg.SendMessage();   
    }

    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/Input/HappyCourseSrhForm.aspx";
        ControllerUtils.Transfer(Context, directUrl, true);
    }

    // check auditing person authority(审核人权限check)
    private Boolean checkAuditingAuthority(string actionId)
    {
        // get employee info
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;




        return true;
    }

    // check person exist
    private Boolean checkExistPerson(string empId)
    {
        HappyPlanDao dao = new HappyPlanDao();
        PersonEmployeeBean beanME = new PersonEmployeeBean();
        beanME.EmployeeId = empId;
        beanME = dao.GetEmployee(beanME);
        if (beanME == null)
        { return false; }

        return true;
    }

    // check authority for update（更新人 登记人 是否同一人） 
    private Boolean checkUpdateAuthority(string actionId)
    {
        if (string.IsNullOrEmpty(actionId))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0901").ToString();
            return false;
        }

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

  

        return true;
    }


    // bind data to controls
    private void bindSelectData(string actionId)
    {
        
    }

    // 取得审批权限
    private int getJobLevel()
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
        PersonEmployeeBean beanEmp = new PersonEmployeeBean();
        HappyPlanDao dao = new HappyPlanDao();
        beanEmp.EmployeeId = emplyId;
        beanEmp = dao.GetEmployeeJob(beanEmp);
        if (beanEmp == null || string.IsNullOrEmpty(beanEmp.JobLimit))
        { return 0; }
        return Convert.ToInt32(beanEmp.JobLimit);
    }

    // reset controls to page
    private void resetFormControls()
    {
        
    }

    // bind common catalog data 
    private void initPageControls()
    {
        
        //if (lstAud2.Items.Count > 0) lstAud2.Items.RemoveAt(0);
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
    }


}
